package com.example.demo.dao.impl;

import com.example.demo.dao.BaseDaoImpl;
import com.example.demo.dao.IProviderDao;
import com.example.demo.dao.IVisaCardDao;
import com.example.demo.model.po.ProviderPage;
import com.example.demo.model.po.VisaPage;
import com.example.demo.model.request.ProviderPageRequest;
import com.example.demo.model.request.VisaPageRequest;
import com.example.demo.util.AssertUtils;
import com.example.demo.util.DateUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.SelectJoinStep;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.example.demo.jooq.tables.ThirdProvider.THIRD_PROVIDER;
import static com.example.demo.jooq.tables.VisaCard.VISA_CARD;
import static org.jooq.impl.DSL.count;

@Component
public class IProviderDaoImpl extends BaseDaoImpl implements IProviderDao {


    @Override
    public List<ProviderPage> queryPageProviderList(ProviderPageRequest providerPageRequest) {
        //查询条件
        List<Condition> conditions = new ArrayList<>();
        if(AssertUtils.isNotEmpty(providerPageRequest.getKeyword())){
            conditions.add(THIRD_PROVIDER.PROVIDER_CONTACT_NAME.likeRegex(providerPageRequest.getKeyword().trim())
                            .or(THIRD_PROVIDER.PROVIDER_COMPANY_NAME.likeRegex(providerPageRequest.getKeyword().trim()))
            );
        }
        if(AssertUtils.isNotEmpty(providerPageRequest.getSite())){
            conditions.add(THIRD_PROVIDER.SITE.likeRegex(providerPageRequest.getSite()));
        }
        SelectJoinStep<Record> totalLine = this.getDslContext()
                .select(THIRD_PROVIDER.fields())
                .select(count().over().as("totalLine"))
                .from(THIRD_PROVIDER);
        totalLine.where(conditions);
        return totalLine.orderBy(THIRD_PROVIDER.CREATED_DATE.desc())
                .offset(providerPageRequest.getOffset())
                .limit(providerPageRequest.getPageSize())
                .fetchInto(ProviderPage.class);
    }
}
